package com.ruoyi.report_tg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.DealHeh;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_tg.domain.TdczbjlbyBaDetail;
import com.ruoyi.report_tg.domain.vo.TdczbjlbyBaDetailVo;
import com.ruoyi.report_tg.mapper.TdczbjlbyBaDetailMapper;
import com.ruoyi.report_tg.service.ITdczbjlbyBaDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_tg.domain.bo.TdczbjlbyBsInfoBo;
import com.ruoyi.report_tg.domain.vo.TdczbjlbyBsInfoVo;
import com.ruoyi.report_tg.domain.TdczbjlbyBsInfo;
import com.ruoyi.report_tg.mapper.TdczbjlbyBsInfoMapper;
import com.ruoyi.report_tg.service.ITdczbjlbyBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 土的承载比(CBR)记录一Service业务层处理
 *
 * @author zyb
 * @date 2024-05-24
 */
@RequiredArgsConstructor
@Service
@Transactional
public class TdczbjlbyBsInfoServiceImpl implements ITdczbjlbyBsInfoService {

    private final TdczbjlbyBsInfoMapper baseMapper;
    private final TdczbjlbyBaDetailMapper detailMapper;


    /**
     * 查询土的承载比(CBR)记录一
     */
    @Override
    @FIllBasicInfo
    public TdczbjlbyBsInfoVo queryById(Long excelId){
        TdczbjlbyBsInfoVo tdczbjlbyBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<TdczbjlbyBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<TdczbjlbyBaDetail>()
            .eq(TdczbjlbyBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            tdczbjlbyBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return tdczbjlbyBsInfoVo;
    }

    /**
     * 查询土的承载比(CBR)记录一列表
     */
    @Override
    public TableDataInfo<TdczbjlbyBsInfoVo> queryPageList(TdczbjlbyBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TdczbjlbyBsInfo> lqw = buildQueryWrapper(bo);
        Page<TdczbjlbyBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询土的承载比(CBR)记录一列表
     */
    @Override
    public List<TdczbjlbyBsInfoVo> queryList(TdczbjlbyBsInfoBo bo) {
        LambdaQueryWrapper<TdczbjlbyBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TdczbjlbyBsInfo> buildQueryWrapper(TdczbjlbyBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TdczbjlbyBsInfo> lqw = Wrappers.lambdaQuery();
//        lqw.eq(bo.getTotalPage() != null, TdczbjlbyBsInfo::getTotalPage, bo.getTotalPage());
//        lqw.eq(bo.getPage() != null, TdczbjlbyBsInfo::getPage, bo.getPage());
//        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), TdczbjlbyBsInfo::getJilbh, bo.getJilbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), TdczbjlbyBsInfo::getJiancdwmc, bo.getJiancdwmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), TdczbjlbyBsInfo::getGongcbwyt, bo.getGongcbwyt());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), TdczbjlbyBsInfo::getYangpmc, bo.getYangpmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), TdczbjlbyBsInfo::getYangpbh, bo.getYangpbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), TdczbjlbyBsInfo::getYangpsl, bo.getYangpsl());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), TdczbjlbyBsInfo::getYangpzt, bo.getYangpzt());
//        lqw.eq(bo.getYangpsj() != null, TdczbjlbyBsInfo::getYangpsj, bo.getYangpsj());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), TdczbjlbyBsInfo::getShiyjcrq, bo.getShiyjcrq());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), TdczbjlbyBsInfo::getShiytjwd, bo.getShiytjwd());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), TdczbjlbyBsInfo::getShiytjsd, bo.getShiytjsd());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), TdczbjlbyBsInfo::getJiancyj, bo.getJiancyj());
//        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), TdczbjlbyBsInfo::getPandyj, bo.getPandyj());
//        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), TdczbjlbyBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), TdczbjlbyBsInfo::getFujsm, bo.getFujsm());
//        lqw.eq(bo.getPengzpjz30() != null, TdczbjlbyBsInfo::getPengzpjz30, bo.getPengzpjz30());
//        lqw.eq(bo.getPengzpjz50() != null, TdczbjlbyBsInfo::getPengzpjz50, bo.getPengzpjz50());
//        lqw.eq(bo.getPengzpjz98() != null, TdczbjlbyBsInfo::getPengzpjz98, bo.getPengzpjz98());
//        lqw.eq(bo.getGanmdpjz30() != null, TdczbjlbyBsInfo::getGanmdpjz30, bo.getGanmdpjz30());
//        lqw.eq(bo.getGanmdpjz50() != null, TdczbjlbyBsInfo::getGanmdpjz50, bo.getGanmdpjz50());
//        lqw.eq(bo.getGanmdpjz98() != null, TdczbjlbyBsInfo::getGanmdpjz98, bo.getGanmdpjz98());
//        lqw.eq(bo.getHanslpjz30() != null, TdczbjlbyBsInfo::getHanslpjz30, bo.getHanslpjz30());
//        lqw.eq(bo.getHanslpjz50() != null, TdczbjlbyBsInfo::getHanslpjz50, bo.getHanslpjz50());
//        lqw.eq(bo.getHanslpjz98() != null, TdczbjlbyBsInfo::getHanslpjz98, bo.getHanslpjz98());
//        lqw.eq(bo.getXislpjz30() != null, TdczbjlbyBsInfo::getXislpjz30, bo.getXislpjz30());
//        lqw.eq(bo.getXislpjz50() != null, TdczbjlbyBsInfo::getXislpjz50, bo.getXislpjz50());
//        lqw.eq(bo.getXislpjz98() != null, TdczbjlbyBsInfo::getXislpjz98, bo.getXislpjz98());
        return lqw;
    }

    /**
     * 新增土的承载比(CBR)记录一
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(TdczbjlbyBsInfoBo bo) {
        TdczbjlbyBsInfo add = BeanUtil.toBean(bo, TdczbjlbyBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细
            ArrayList<TdczbjlbyBaDetail> details = new ArrayList<>(9);
            Long[] jiscsArray = new Long[]{30L, 50L, 98L};
            Long[] shiybhArray = new Long[]{1L, 2L, 3L};
            for (long jiscs : jiscsArray) {
                for (long shiybh : shiybhArray) {
                    details.add(new TdczbjlbyBaDetail(add.getExcelId(), jiscs, shiybh));
                }
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改土的承载比(CBR)记录一
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @DealHeh
    public Boolean updateByBo(TdczbjlbyBsInfoBo bo) {
        TdczbjlbyBsInfo update = BeanUtil.toBean(bo, TdczbjlbyBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), TdczbjlbyBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TdczbjlbyBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除土的承载比(CBR)记录一
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
